package com.txsj.scan.activity.allocationapply;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.txsj.scan.R;
import com.txsj.scan.base.BaseActivity;
import com.txsj.scan.base.BasePresenter;

import java.util.ArrayList;
import java.util.HashMap;

public class AllocationStateListActivity extends BaseActivity<BasePresenter> {
    @Override
    protected BasePresenter providePresenter() {
        return null;
    }

    ListView lv_state;
    PopupListAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_allocation_state_list);
        lv_state = (ListView) findViewById(R.id.lv_state);
        getStateList();
        adapter = new PopupListAdapter();
        lv_state.setAdapter(adapter);

        lv_state.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = getIntent();
                intent.putExtra("result", new Gson().toJson(stateList.get(position)));
                setResult(0x185, intent);
                finish();
            }
        });
    }

    ArrayList<HashMap<String, String>> stateList = new ArrayList<>();

    //    （-1待审核 -2待出库 -3待入库 -4驳回 -5已完成 -6已作废）
    private void getStateList() {
        HashMap<String, String> map1 = new HashMap<>();
        map1.put("stateName", "待审核");
        map1.put("state", "-1");
        stateList.add(map1);
        HashMap<String, String> map2 = new HashMap<>();
        map2.put("stateName", "待出库");
        map2.put("state", "-2");
        stateList.add(map2);
        HashMap<String, String> map3 = new HashMap<>();
        map3.put("stateName", "待入库");
        map3.put("state", "-3");
        stateList.add(map3);
        HashMap<String, String> map6 = new HashMap<>();
        map6.put("stateName", "驳回");
        map6.put("state", "-4");
        stateList.add(map6);
        HashMap<String, String> map4 = new HashMap<>();
        map4.put("stateName", "已完成");
        map4.put("state", "-5");
        stateList.add(map4);
        HashMap<String, String> map5 = new HashMap<>();
        map5.put("stateName", "已作废");
        map5.put("state", "-6");
        stateList.add(map5);
    }


    class PopupListAdapter extends BaseAdapter {

        public PopupListAdapter() {

        }

        @Override
        public int getCount() {
            if (stateList != null && stateList.size() > 0) return stateList.size();
            return 0;
        }

        @Override
        public Object getItem(int position) {
            return stateList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            PopupListAdapter.ViewHolder viewHolder = null;
            if (viewHolder == null) {
                convertView = View.inflate(AllocationStateListActivity.this, R.layout.item_array_list, null);
                viewHolder = new PopupListAdapter.ViewHolder(convertView);
                convertView.setTag(viewHolder);
            } else {
                viewHolder = (PopupListAdapter.ViewHolder) convertView.getTag();
            }

            HashMap<String, String> map = ((HashMap<String, String>) stateList.get(position));
            viewHolder.tv_item.setText(map.get("stateName"));
            return convertView;
        }


        class ViewHolder {
            TextView tv_item;
            public ViewHolder(View viewItem) {
                tv_item = viewItem.findViewById(R.id.tv_item);
            }
        }
    }
}
